<?php
class IndexAction extends CommonAction {
	// 框架首页
	public function index() {
		ob_clean();
		$this->_checkUser();
		$this->_Config_name();//调用参数
		C ( 'SHOW_RUN_TIME', false ); // 运行时间显示
		C ( 'SHOW_PAGE_TRACE', false );
		$fck = D ('Fck');
    
    //清空每日时间
    // $fck->query("UPDATE `xt_fck` SET `day_feng`=0,_times=".$nowdate." WHERE _times !=".$nowdate."");
		$id = $_SESSION[C('USER_AUTH_KEY')];
		$field = '*';
		$fck_rs = $fck->field($field)->find($id);
        $AGENT="";
        $this->_getAgentConfirm($AGENT,1);
        $this->assign('AGENT',$AGENT);//会员级别
		$HYJJ="";
		$this->_levelConfirm($HYJJ,1);
		$this->assign('HYJJ',$HYJJ);//会员级别
        $HYMN="";
        $this->_getLevelConfirm($HYMN,1);
        $this->assign('HYMN',$HYMN);//会员级别金额
        $this->assign('fck_rs',$fck_rs);

        $fee_ruel = $this->getFeeRule();

        //判断申请状态
        //$this->getLevelStatus($fck_rs);


        //判断级别名
        $level_name = $this->getLevelName($fck_rs);
        $this->assign('level_name',$level_name);//会员级别金额

        $rs = M('fck')
            ->alias('f')
            ->join('xt_gouwu AS g ON f.id = g.uid')
            ->where(['re_id' => $id, 'ispay' => 1])
            ->field('f.id,f.user_id,g.id,g.ispay')
            ->sum('cprice');
        $term_price = isset($rs) ? $rs : 0;
        $this->assign('term_price',$term_price);//团队业绩金额

        $n_point = M('sort_tree_one')->where(['uid' => $id])->count();
        $this->assign('n_point',$n_point);//排队点位

        $this->display();

	}
	//用户协议的判断
    public function get_xieyi(){
		$_SESSION['modal_agreement']=0;
	}
	public function quXiao(){
		$_SESSION=array();
	}

	 public function yanzheng1() {
        $code=$_GET['code'];
        $url="https://api.weixin.qq.com/sns/oauth2/access_token?";
        $post_data="appid=wxdfc6fec1902c9870&secret=2f6643d8d42a16646da60953e61f2af2&code=".$code."&grant_type=authorization_code";

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
        curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false );
       
        $result = curl_exec($ch);
         if(curl_errno($ch)){
            // 显示报错信息；终止继续执行
            die(curl_error($ch));
        }
        curl_close($ch);
         $result2 = json_decode($result,true);

        $dd=$result2['access_token'];
        $pid=$result2['openid'];
        $_SESSION['login_count']        =   $pid;
        $_SESSION['login_isAgent']      =   $dd;
    
   
  
      
      
     
        $url1="https://api.weixin.qq.com/sns/userinfo?";
        $post_data1="access_token=".$dd."&openid=".$pid."&lang=zh_CN";
        $ch1 = curl_init();
        curl_setopt($ch1, CURLOPT_POST, 1);
        curl_setopt($ch1, CURLOPT_HEADER, 0);
        curl_setopt($ch1, CURLOPT_URL, $url1);
        curl_setopt($ch1, CURLOPT_POSTFIELDS, $post_data1);
        curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1); 
        curl_setopt ($ch1, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt ($ch1, CURLOPT_SSL_VERIFYPEER, false );
       
        $result1 = curl_exec($ch1);
       $data = json_decode($result1,true);

      $fck=M('fck');
      $time=time();
     

      $a=$fck->where("user_id='".$pid."'")->find();
      if(!$a){

       // $aid=$_SESSION['loginUseracc'];
       

          $tt=array();
          $tt['user_id']=$pid;
          $tt['nickname']=$data['nickname'];
          $tt['get_ceng']=$data['sex'];
          $tt['us_img']=$data['headimgurl'];
          $tt['u_level']=0;
          $tt['pdt']    =$time;
          $tt['new_login_time']=$time;
          $rs=$fck->add($tt);
          if($rs){
            $aa=$fck->where("user_id='".$pid."'")->find();
            $_SESSION[C('USER_AUTH_KEY')]    =   $aa['id']; 
          }
        }
        
    else{
       $fck->execute("update __TABLE__ set nickname='".$data['nickname']."',us_img='".$data['headimgurl']."',get_ceng=".$data['sex'].",new_login_time=".$time." where user_id='".$pid."'");
      
       $_SESSION[C('USER_AUTH_KEY')]    =   $a['id'];
      
        
    }

   $this->index();

    } 

	public function Service(){
	    $fee_rs = M('fee')->find();
	    $this->assign('fee_rs',$fee_rs);
	    $this->display();
    }

    /**
     * 判断级别
     * @param $fck_rs
     */
    private function getLevelName($fck_rs) {
        if($fck_rs['is_agent'] == 2 && $fck_rs['shoplx'] == 0) {
            $level_name = '总运营中心';
        } else if($fck_rs['is_agent'] == 2 && $fck_rs['shoplx'] == 1) {
            $level_name = '市级运营中心';
        } else if($fck_rs['is_agent'] == 2 && $fck_rs['shoplx'] == 2) {
            $level_name = '体验中心';
        } else if($fck_rs['is_pay'] == 1 && $fck_rs['is_agent'] !== 2) {
            $level_name = '正式会员';
        } else if($fck_rs['is_pay'] == 0 && $fck_rs['is_agent'] == 1 && $fck_rs['is_aa'] == 1) {
            $level_name = '待审核代理';
        } else {
            $level_name = '非会员';
        }
        return $level_name;
    }

    /**
     * 判断级别申请状态
     * @param $fck_rs
     */
    private function getLevelStatus($fck_rs) {
        if( $fck_rs['is_agent'] == 0 && $fck_rs['is_pay'] == 0 && $fck_rs['cpzj'] == 0 && !$_SESSION['is_first_check']) {
            //sleep(1);
            $_SESSION['is_first_check'] = true; //用户第一次登录检测是否已开通充值

            //$url = U("Index/cody", ['c_id' => 1]);
            $url = U("User/applyForMember");
            $this->_box(1,'先申请成为正式会员 或者 申请服务中心！ 后充值等待审核',$url,1);
            exit;
        }

        if( $fck_rs['is_agent'] == 1 && $fck_rs['is_pay'] == 0 && $fck_rs['cpzj'] > 0 &&  !$_SESSION['is_first_check']) {
            $_SESSION['is_first_check'] = true; //用户第一次登录检测是否已开通充值

            $url = U("Index/index");
            $this->_box(1,'服务中心已申请，等待审核',$url,1);
            exit;
        }
        if( $fck_rs['is_agent'] == 0 && $fck_rs['is_pay'] == 0 && $fck_rs['cpzj'] > 0 &&  !$_SESSION['is_first_check']) {
            $_SESSION['is_first_check'] = true; //用户第一次登录检测是否已开通充值

            $url = U("Index/index");
            $this->_box(1,'已申请正式会员，等待审核',$url,1);
            exit;
        }
    }

    public function addtest() {
        //ignore_user_abort(true);
        set_time_limit(0);
        @ini_set('implicit_flush',1);
        ob_implicit_flush(1);
        $xml = M('xml');

        for($i = 0; $i < 30000; $i++) {
            dump($i);
            dump( 'befroe --' . $this->convert(memory_get_usage(TRUE)));
            dump( 'befroe --' . $this->convert(memory_get_usage()));
            $data = [];
            $data['money'] = $i;
            $data['amount'] = $data['amount'] + $i;
            $data['x_date'] = time();
            $data['is_lock'] = 0;
            $r = $xml->add($data);
            $p = &$r;
            ob_flush();
            flush();
            dump( 'middle --' . $this->convert(memory_get_usage(TRUE)));
            dump( 'middle --' . $this->convert(memory_get_usage()));
            unset($r);
            $data = NULL;
            dump('----$r---p' .$p);

            dump( 'unset --varible--' . $this->convert(memory_get_usage(TRUE)));
            dump( 'unset --varible--' . $this->convert(memory_get_usage()));

            //sleep(1);

            /*if($i == 12000){
                ignore_user_abort(false);
                set_time_limit(1);
            }*/
        }
    }

    function convert($size)
    {
        $unit=array('b','kb','mb','gb','tb','pb');
        return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
    }

    public function testMfunc() {
        set_time_limit(0);
        $i = 0;
       /* @ini_set('implicit_flush',1);
        ob_implicit_flush(1);*/
        while(true) {
            $i++;
           if( $i >= 30) break;
            echo str_repeat(' ',1024*64);
            dump(date("h:i:s"));
            echo memory_get_usage();
           sleep(1);
       }
        /*M('fck');
        M('fck');*/


        return true;
    }
}
?>